package com.gopay.bis.manager.bank.Wap;

import java.util.Date;
import java.util.Map;

import javax.annotation.Resource;

import com.gopay.bis.common.util.BankLogUtils;
import com.gopay.bis.common.util.BisPayCheckUtils;
import com.gopay.bis.common.util.DateUtils;
import com.gopay.common.constants.bank.BankConstants;
import com.gopay.common.domain.bank.BaseMessage;
import com.gopay.common.domain.bank.cmb.ebank.pay.CmbB2cMessage;
import com.gopay.common.domain.bank.cmb.ebank.query.CmbB2cResMessage;
import com.gopay.common.exception.GopayException;
import com.gopay.remote.bank.WapPayManagerRemoteService;
import com.gopay.remote.bank.businesses.BankService;

/**
 * 招商银行支付网关B2C对象封装
 * 
 * @CmbWapB2cManager.java
 * @author sunxiaojian 北京 11月23日:阴有雾 4～8℃ 明:阴 5～9℃
 * 
 */

public class CmbWapB2cManager implements WapPayManagerRemoteService {

	
	@Resource (name="cmbB2cBankServiceClient")
	private BankService cmbB2cBankServiceClient;
	
	@Resource(name = "cmbB2cBankServiceClientHna")
    private BankService cmbB2cBankServiceClientHna;
	
	@Resource (name = "cmbConfig")
	private Map<String , String> cmbConfig;
	
	/**
	 * 招商银行 B2C签名
	 * @param 
	 * @return CmbB2cMessage
	 */
	@Override
	public Object sign(BaseMessage msg) throws GopayException {
		msg.setBankOrderId(msg.getOrderId().substring(2, 4) + msg.getOrderId().substring(8));  	
        CmbB2cMessage message = (CmbB2cMessage) msg;
        message.setBranchID(message.getBranchCode());
        String cono = buildMerchantNo(message);
      	message.setCoNo(cono);
        message.setBillNo(msg.getBankOrderId());
        message.setAmt(message.getAmount().toString());
        // 支付日期为八位YYYYMMDD
        message.setDate(message.getOrderDate().substring(0, 8));
        // 区分充值/支付交易
        if (message.isSupportCredit()) {
            // 网上支付
            message.setGoodsType("54011600");
        } else {
            // 信用卡还款
            message.setGoodsType("54010306");
        }
        message.setMerchantUrl(message.getPayRuturnUrl());
		
        // 提交银行地址
        message.setBankUrl(message.getPayUrl());
        // 调用远程接口请求签名数据
        BankLogUtils.printB2CManagerSignSendLog(message);
//        buildMessageObj(message);
        String signVal;
        if(message.isHna()){
    		signVal = (String) cmbB2cBankServiceClientHna.sign(message);
	    }else{
	    	signVal = (String) cmbB2cBankServiceClient.sign(message);
	    }
        message.setMerchantCode(signVal);
        BankLogUtils.printB2CManagerSignReturnLog(message);
		return msg;
	}

	/**
	 * 招商银行  验证签名
	 */
	@Override
	public Object verify(BaseMessage msg) throws GopayException {
		CmbB2cMessage cmbB2cMessage = (CmbB2cMessage) msg;
		BankLogUtils.printB2CManagerVerifySendLog(cmbB2cMessage);
        boolean isVerify;
        if(cmbB2cMessage.isHna()){
        	isVerify = (Boolean) cmbB2cBankServiceClientHna.verify(cmbB2cMessage);
	    }else{
            isVerify = (Boolean) cmbB2cBankServiceClient.verify(cmbB2cMessage);
	    }
        BankLogUtils.printB2CManagerVerifyReturnLog(cmbB2cMessage,isVerify);
        return isVerify;
	}

	
	/**
	 * 查询订单详细信息
	 * @param BaseMessage
	 * @return Result
	 */
	@Override
	public Object query(Object obj) throws GopayException {
		CmbB2cMessage message = (CmbB2cMessage) obj;
        message.setBranchID(message.getBranchCode());
        String cono = buildMerchantNo(message);
      	message.setCoNo(cono);
        message.setPwd(message.getPrivatePwd());
        if(BisPayCheckUtils.checkExternalOrderID(message.getOrderId())){
        	 message.setDate(message.getOrderDate().substring(0,8));
        	 message.setBillNo(message.getBankOrderId());
        }
        CmbB2cResMessage queryResMsg = null;
        BankLogUtils.printB2CManagerQuerySendLog(message, message);
        if(message.isHna()){
        	queryResMsg = (CmbB2cResMessage) cmbB2cBankServiceClientHna.query(message);
	    }else{
	    	queryResMsg = (CmbB2cResMessage) cmbB2cBankServiceClient.query(message);
	    }
        queryResMsg.setOutStlmCd(BankConstants.BANK_LOGO_CMB);
        BankLogUtils.printB2CManagerQueryReturnLog(queryResMsg);
        return queryResMsg;
	}

	public String buildMerchantNo(CmbB2cMessage msg){
   	 //默认为大额商户号
		//只支持小额
     	String cono = msg.getMerID2nd();//msg.getMerID();
//     	
//     	if(msg.isSmallMer()){
//     	  cono = (String) cmbConfig.get("CMB.B2C.CONO.SMALL.AMOUNT");
//     	}
     	
     	return cono;
   }
	
//	/**
//	 * 加签报文信息
//	 * @param cmbB2cMessage
//	 */
//	public void buildMessageObj(CmbB2cMessage cmbB2cMessage) {
//        // 加签报文信息
//        StringBuffer plainStr = new StringBuffer();
//        plainStr.append(cmbB2cMessage.getDate()).append("|");
//        plainStr.append(cmbB2cMessage.getBranchID()).append("|");
//        plainStr.append(cmbB2cMessage.getCoNo()).append("|");
//        plainStr.append(cmbB2cMessage.getBillNo()).append("|");
//        plainStr.append(cmbB2cMessage.getAmt()).append("|");
//        plainStr.append(cmbB2cMessage.getMerchantUrl()).append("|");
//        plainStr.append(cmbB2cMessage.getGoodsType()).append("|");
//        cmbB2cMessage.setPlainMessage(plainStr.toString());
//    }
//	
	/**
	 * 招商银行  日终结算
	 * @author Object
	 */
	@Override
	public Object dayend(BaseMessage base) throws GopayException, Exception {
		return null;
	}

}
